package db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.log4j.Logger;


public class DbApprovings {
    public static Logger LOG = Logger.getLogger("db.DbApprovings");

    public static boolean addApprovings(int wordID, int userID, String strMeaning) {
        boolean bOK = false;
        Connection cnn = null;
        CallableStatement cstat = null;
        try {
            cnn = DbController.getConnection();
            cstat = cnn.prepareCall("{call m_approvings_insert(?, ?, ?)}");
            cstat.setInt(1, wordID);
            cstat.setInt(2, userID);
            cstat.setString(3, "N'" + strMeaning + "'");
            bOK = (cstat.executeUpdate() == 1);
        } catch (Exception ex) {
            LOG.error("addApprovings", ex);
        } finally {
            try {
                if (cstat != null) {
                    cstat.close();
                }
                if (cnn != null) {
                    cnn.close();
                }
            } catch (Exception ex) {
            }
        }
        return bOK;
    }

    public static boolean addApprovings(String word, int dictID, int userID, String strMeaning) {
        boolean bOK = false;
        Connection cnn = null;
        PreparedStatement pstat = null;
        ResultSet rs = null;
        int wordID = 0;
        try {
            cnn = DbController.getConnection();
            DbWords.addWord(word, strMeaning, dictID);
            pstat = cnn.prepareStatement("select m_id from m_words where m_dictID=" + dictID + " and m_word=N'" + word
                    + "'");
            rs = pstat.executeQuery();
            if (rs.next()) {
                wordID = rs.getInt("m_id");
                bOK = DbApprovings.addApprovings(wordID, userID, strMeaning);
            }
        } catch (Exception ex) {

            LOG.error("addApprovings(String word...)", ex);
        } finally {
            try {
                if (pstat != null) {
                    pstat.close();
                }
                if (cnn != null) {
                    cnn.close();
                }
            } catch (Exception ex) {
            }
        }
        return bOK;
    }
}
